﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace TestJQGrid
{
    public partial class ReportCreation : System.Web.UI.Page
    {
        #region initialize & load default report
        TMReports tm;

        protected void Page_Load(object sender, EventArgs e)
        {
            tm = TMReports.Instance;
        }

        private void loadReport2Grid()
        {
            //create new has been reset
            DataTable dt = tm.getData();
            LBReports.SelectedValue = tm.getReportId().ToString();
            TBName.Text = tm.getReportName();
            TBEmail.Text = tm.getEmailID();
            DDLFrequency.SelectedValue = tm.getFrequency();
            DDLPPType.SelectedValue = tm.getType();

            JQGrid1.DataSource = dt;
            JQGrid1.DataBind();

            ManageEditable(false);
        }

        protected void LBReports_DataBound(object sender, EventArgs e)
        {
            loadReport2Grid();
        }

        protected void LBReports_SelectedIndexChanged(object sender, EventArgs e)
        {
            tm.CreatingNew(false);
            tm.Reload(Convert.ToInt32(LBReports.SelectedValue));
            ManageEditable(false);
            loadReport2Grid();
        }

        private void ManageEditable(bool editable)
        {
            //manage top & left
            TBName.Enabled = editable;
            TBEmail.Enabled = editable;
            //LBReports.Enabled = editable;
            DDLPPType.Enabled = editable;
            DDLFrequency.Enabled = editable;

            //manage buttons
            BApply.Visible = editable;
            BCancel.Visible = editable;
            BEdit.Visible = !editable;
            BCreate.Visible = !editable;

            //manage jqgrid
            if (!editable)
                JQGrid1.ToolBarSettings.ToolBarPosition = Trirand.Web.UI.WebControls.ToolBarPosition.Hidden;
            else
                JQGrid1.ToolBarSettings.ToolBarPosition = Trirand.Web.UI.WebControls.ToolBarPosition.Bottom;
        }
        #endregion

        #region buttons at work
        protected void BCancel_Click(object sender, EventArgs e)
        {
            tm.CreatingNew(false);
            tm.Reload(Convert.ToInt32(LBReports.SelectedValue));
            loadReport2Grid();
        }

        protected void BEdit_Click(object sender, EventArgs e)
        {
            ManageEditable(true);
            //RESOLVE THIS ISSUE LATER
            TBName.Text = tm.getReportName();
            TBEmail.Text = tm.getEmailID();
            DDLFrequency.SelectedValue = tm.getFrequency();
            DDLPPType.SelectedValue = tm.getType();

        }

        protected void BCreate_Click(object sender, EventArgs e)
        {
            tm.CreatingNew(true);
            ManageEditable(true);
            TBName.Text = "";
            TBEmail.Text = "";
        }

        protected void BApply_Click(object sender, EventArgs e)
        {
            //perform validation checks before save
            if (!validRuleDetails()) return;
            //update the db
            tm.setEmailId(TBEmail.Text);
            tm.setFrequency(DDLFrequency.SelectedValue.ToString());
            tm.setType(DDLPPType.SelectedValue.ToString());
            tm.UpdateDB(TBName.Text);
            //reset create new state
            tm.CreatingNew(false);
            //bind the lbreport to get newly added report name
            LBReports.DataBind();
            //reload the grid
            loadReport2Grid();
        }

        private bool validRuleDetails()
        {
            //check report name to be not null
            if (String.IsNullOrEmpty(TBName.Text))
            {
                WebMsgBox.Show("Please enter Report Name.");
                return false;
            }

            //check that it has atleast one entry
            if (tm.getRowCount() <= 0)
            {
                WebMsgBox.Show("Report needs atleast 1 field.");
                return false;
            }

            return true;
        }
        #endregion

        #region jqgrid operations
        protected void JQGrid1_RowAdding(object sender, Trirand.Web.UI.WebControls.JQGridRowAddEventArgs e)
        {
            if (!tm.addRow(e.RowData))
                WebMsgBox.Show("The Added Column is not valid. Please add a valid statement.");
        }

        protected void JQGrid1_RowEditing(object sender, Trirand.Web.UI.WebControls.JQGridRowEditEventArgs e)
        {
            if (!tm.editRow(Convert.ToInt32(e.RowKey), e.RowData))
                WebMsgBox.Show("The Edited Column is not valid.");
        }

        protected void JQGrid1_RowDeleting(object sender, Trirand.Web.UI.WebControls.JQGridRowDeleteEventArgs e)
        {
            tm.deleteRow(Convert.ToInt32(e.RowKey));
        }
        #endregion
    }
}